<?php
namespace App\Helpers;

use Illuminate\Support\Facades\URL;
use Symfony\Component\HttpFoundation\Response as FoundationResponse;
use Response;

/**
 * 接口响应
 * @package App\Helpers
 */
trait ApiResponse
{
    /**
     * 返回状态码
     * @var int
     */
    protected $resStatusCode = FoundationResponse::HTTP_OK;
    /**
     * @return mixed
     */
    public function getStatusCode()
    {
        return $this->resStatusCode;
    }

    /**
     * @param $statusCode
     * @return $this
     */
    public function setStatusCode($statusCode)
    {
        $this->resStatusCode = $statusCode;
        return $this;
    }

    /**
     * 显示信息
     * @param string $type
     * @param string $msg
     * @param array $otherData
     * @param string $url
     * @return \Illuminate\Http\JsonResponse
     */
    public function showMsg($type = 'success', $msg = '',$otherData = [], $url = '')
    {
        $data = [];
        $data['status'] = $type;
        switch ($type) {
            case 'success':
                $data['title'] = '操作成功';
                break;
            case 'error':
                $data['title'] = '操作失败';
                break;
            case 'info':
                $data['title'] = '消息提示';
                break;
            case 'warning':
                $data['title'] = '错误警告';
                break;
            default:
                $data['title']  = '提示';
        }
        $data['message']     = $msg;
        $data['data']  = $otherData;
        return Response::json($data, $this->resStatusCode);
    }
}